package mest.controller.cxj;


import com.alibaba.fastjson.JSON;
import mest.VO.cxj.WarehouseSaveManytomanyRecordVO;
import mest.entity.warehousesave_manytomany_record;
import mest.service.Modeling.IwarehouseFileService;
import mest.service.cxj.WarehouseSaveManytomanyRecordService;
import mest.service.system.DBService;
import mest.util.ResultObj;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("warehouseSave")
@CrossOrigin
public class WarehouseSaveController {

    @Autowired
    private WarehouseSaveManytomanyRecordService warehouseSaveManytomanyRecordService;

    @Autowired
    private IwarehouseFileService iwarehouseFileService;

    @Autowired
    private DBService dbService;

    /**
     * 出入库操作
     */
    @PostMapping("operation")
    public String operation(@RequestBody warehousesave_manytomany_record wsmr) {
        String theirDb = dbService.getCompanyDB();//获取数据库名称
        Integer userId = dbService.getUserId(); //获取操作人ID
        String status = wsmr.getStatus(); //获取操作状态
        int currentNumber = iwarehouseFileService.getNumber_WarehouseAndSave(wsmr.getWsmId(), theirDb); //获取当前库存数量
        Integer editnumber = wsmr.getNumber(); //获取要修改的数量
        ResultObj resultObj = null;
        try {
            int resultNumber = iwarehouseFileService.countNumber_WarehouseAndSave(status, currentNumber, editnumber);//计算结果
            int num = iwarehouseFileService.updateNumber_WarehouseAndSave(wsmr.getWsmId(), resultNumber, theirDb); //修改库存
            if (num > 0) {
                wsmr.setEfId(userId); //设置操作人
                if (warehouseSaveManytomanyRecordService.insertRecord(wsmr, theirDb) > 0) {
                    resultObj = ResultObj.builder().code(0).msg("操作成功").build();
                }
            } else {
                throw new RuntimeException("操作失败");
               // resultObj = ResultObj.builder().code(-1).msg("操作失败").build();
            }
        } catch (Exception e) {
            e.printStackTrace();
            resultObj = ResultObj.builder().code(-2).msg("系统异常,请联系管理员").build();
        }
        return JSON.toJSONString(resultObj);
    }


    /**
     * 查询出入库操作明细
     */
    @GetMapping("detail")
    public String detail(@RequestParam Integer page, @RequestParam Integer limit, @RequestParam Integer wfId) {
        ResultObj resultObj = null;
        String theirDb = dbService.getCompanyDB();
        try {
            List<WarehouseSaveManytomanyRecordVO> recordList = warehouseSaveManytomanyRecordService.selectAllRecordByWfId(page, limit, wfId, theirDb);
            Integer count = warehouseSaveManytomanyRecordService.selectCountRecordByWfId(wfId, theirDb);
            resultObj = new ResultObj(0, "success", count, recordList);
        } catch (Exception e) {
            e.printStackTrace();
            resultObj = ResultObj.builder().code(-2).msg("系统异常,请联系管理员").build();
        }
        return JSON.toJSONString(resultObj);
    }


}
